package com.googlecode.dgwt.client.dojo.data.format;

import com.googlecode.dgwt.client.dijit.Dijit;
import com.googlecode.dgwt.client.dojo.exception.ValidationException;

/**
 * @author      Wellington Tadeu dos Santos
 * @since       DGWT v0.01
 */
public class FloatFormatter extends BaseFormatter<Float> {

    private static FloatFormatter formatter;
    
    protected FloatFormatter() {
    }
    
    @Override
    public String formatValue(Float value) {
        return value == null ? null : formatDouble(value);
    }

    @Override
    public Float unformatValue(String value) throws ValidationException {
        try {
            return value == null || (value=value.trim()).isEmpty() ? null : Float.valueOf((float)unformatDouble(value));
        } catch (NumberFormatException e) {
            throw new ValidationException(Dijit.MESSAGE_RESOURCES.valueInvalid(value, "{0}")) ;
        }
    }

    public static FloatFormatter get() {
        if (formatter == null) {
            formatter = new FloatFormatter();
        }
        return formatter;
    }

}
